package com.gmail.dengtao.joe.commons.util;

import java.lang.management.ManagementFactory;
import java.util.regex.Pattern;

/**
 * System Utils: pid, cpu count and etc.
 * @author <a href="mailto:joe.dengtao@gmail.com">DengTao</a>
 * @version 1.0
 * @since 1.0
 */
public class SystemUtils {
	
	private static final String OS_KEY = "os.name";
	private static final String OS = System.getProperty( OS_KEY );
	/** FreeBSD family of OS's */
	// private static final String FREEBSD_REGEX = ".*freebsd.*";
    private static final Pattern FREEBSD_PATTERN = Pattern.compile(".*freebsd.*");
	/** GNU/Linux family of OS's */
	// private static final String LINUX_REGEX = ".*linux.*";
	private static final Pattern LINUX_PATTERN = Pattern.compile(".*linux.*");
	/** Mac OS and Mac OS X operating systems */
	// private static final String MAC_REGEX = "mac.*";
	private static final Pattern MAC_PATTERN = Pattern.compile("mac.*");
	/** Solaris operating system */
	// private static final String SOLARIS_REGEX = "sunos.*|solaris.*";
	private static final Pattern SOLARIS_PATTERN = Pattern.compile("sunos.*|solaris.*");
	/** Windows family of OS's */
	// private static final String WINDOWS_REGEX = ".*windows.*";
	private static final Pattern WINDOWS_PATTERN = Pattern.compile(".*windows.*");
	
	/**
	 * This value may change during a particular invocation of the virtual machine. 
	 * Applications that are sensitive to the number of available processors should therefore occasionally poll this property and adjust their resource usage appropriately. 
	 * @return the maximum number of processors available to the virtual machine; never smaller than one
	 * @since 1.0
	 */
	public static int getCpus() {
		return Runtime.getRuntime().availableProcessors();
	}
	
	/**
	 * get current pid
	 * @return the process id (pid) of current running Java process
	 * @since 1.0
	 */
	public static long getPid() {
		String processName = ManagementFactory.getRuntimeMXBean().getName();
		return Long.parseLong(processName.split("@")[0]);
	}
	
	public static String getOperatingSystem() {
		return OS;
	}
	
	/**
	 * Check current system is FreeBSD family.
	 * @return return true only current system is FreeBSD family.
	 * @since 1.0
	 */
	public static boolean isFreeBSD() {
		return FREEBSD_PATTERN.matcher(OS.toLowerCase()).matches();
	}
	
	/**
	 * Check current system is Linux family.
	 * @return return true only current system is Linux family.
	 * @since 1.0
	 */
	public static boolean isLinux() {
		return LINUX_PATTERN.matcher(OS.toLowerCase()).matches();
	}
	
	/**
	 * Check current system is Mac OS family.
	 * @return return true only current system is Mac OS family.
	 * @since 1.0
	 */
	public static boolean isMac() {
		return MAC_PATTERN.matcher(OS.toLowerCase()).matches();
	}
	
	/**
	 * Check current system is Solaris family.
	 * @return return true only current system is Solaris family.
	 * @since 1.0
	 */
	public static boolean isSolaris() {
		return SOLARIS_PATTERN.matcher(OS.toLowerCase()).matches();
	}
	
	/**
	 * Check current system is Windows family.
	 * @return return true only current system is Windows family.
	 * @since 1.0
	 */
	public static boolean isWindows() {
		return WINDOWS_PATTERN.matcher(OS.toLowerCase()).matches();
	}
}